package com.szsh.aiot.hsm.utils;

import java.security.SecureRandom;

public class RandomUtil {

    private static final SecureRandom RAND = new SecureRandom();

    /**
     * 获取随机字母或者数字
     *
     * @param bound
     * @return
     */
    public static String getRandomLetter(int bound) {
        // 随机整数
        int number = RAND.nextInt(bound);
        number = number % 62;

        if (number < 10) {
            number += 48;
        } else if (number < 36) {
            number += 55;
        } else {
            number += 61;
        }
        return String.valueOf((char) number);
    }

    /**
     * 获取16进制随机数
     *
     * @param len
     * @return
     */
    public static String randomHexString(int len) {
        try {
            StringBuffer result = new StringBuffer();
            for (int i = 0; i < len; i++) {
                result.append(Integer.toHexString(RAND.nextInt(16)));
            }
            return result.toString();
        } catch (Exception e) {
            throw new RuntimeException("create random Failed ", e);
        }

    }
}
